﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.Log;
using Infrastructure.Extension;
using ZICModel.Common;
namespace ZICModel.Fault
{
    public class SubFaultDetail
    {
        public string Description { get; set; }//故障描述
        public string SrcFaultCode { get; set; }//原始故障码
        public string ConvertedCode { get; set; }//转换的故障码
        public string FaultStatus { get; set; }//故障 状态
        public string TroubleExplain { get; set; }//常发、偶发等信息

        private string srcProeprtyDataString;
        private string codeVersion;

        public string[] versionList = new string[] { "00", "01", "03", "04", "05" };

        public SubFaultDetail(string srcString, string version)
        {
            srcProeprtyDataString = srcString;
            codeVersion = version;
        }

        public void Process()
        {
            
            ConvertFaultCode();
            
            ConvertFaultType();
            
            CallLogger.LogMessage(typeof(SubFaultDetail), string.Format(" \t \t SubFault SrcCode:{0} ConvertCode:{1} SubFaultStatus:{2} TroubleExplain {3}", SrcFaultCode, ConvertedCode, FaultStatus,TroubleExplain));
        }

        public void ConvertFaultCode()
        {

            SrcFaultCode = srcProeprtyDataString.Substring(0, 6);

            if (versionList.Contains(codeVersion))
            {
                //0 ，1，3，4,5 号码库得到十六进制故障码后 直接转为十进制即可
                //如：0003D2 转为十进制是：978不足5位在前边补0 ，那么最后结果为：00978
                ConvertedCode = SrcFaultCode.HexString2Int().ToString().PadLeft(5, '0');
            }
            else if (codeVersion == "02")
            {
                //2号码库 得到十六进制后无需转换 需要在故障码表中查询到相应的UDScode，
                ConvertedCode = SrcFaultCode;
            }
            else if (codeVersion == "06")
            {
                //6号码库得到十六进制故障码后需根据以下公式进行换算，不足4位补0后在加P C B U;

                int srcCode = Convert.ToInt32(SrcFaultCode, 16);
                string hexString = string.Empty;
                //十六进制范围             转换公式

                if (0x0000 <= srcCode && srcCode <= 0x3fff)
                {
                    //0x0000--0x3FFF 无需处理  前面直接加字母P （P0000--P3FFF）例如 0102  无需处理    在前边加P是P0102 
                    hexString = (srcCode - 0).ToString();
                    ConvertedCode = string.Concat("P", hexString.PadLeft(4-hexString.Length, '0'));
                }
                else if (0x4000 <= srcCode && srcCode <= 0x7fff)
                {
                    //0x4000--0x7FFF           实际值减去0x4000后的剩余值  前面直接加字母C （C0000--C3FFF）         例如 4001  减去 0x4000 在前边加C 是C0001
                    hexString = (srcCode - 0x4000).ToString();
                    ConvertedCode = string.Concat("C", hexString.PadLeft(4 - hexString.Length, '0'));

                }
                else if (0x8000 <= srcCode && srcCode <= 0xBfff)
                {
                    //0x8000--0xBFFF           实际值减去0x8000后的剩余值  前面直接加字母B （B0000--B3FFF）         例如 8005  减去 0x8000 在前边加B 是B0005
                    hexString = (srcCode - 0x8000).ToString();
                    ConvertedCode = string.Concat("B", hexString.PadLeft(4 - hexString.Length, '0'));
                }
                else if (0xC000 <= srcCode && srcCode <= 0xFfff)
                {
                    //0xC000--0xFFFF           实际值减去0xC000后的剩余值  前面直接加字母U （U0000--U3FFF）         例如 C122  减去 0xC000 在前边加U 是U0122
                    hexString = (srcCode - 0xC000).ToString();
                    ConvertedCode = string.Concat("U", hexString.PadLeft(4 - hexString.Length, '0'));
                }
                else
                {
                    CallLogger.LogMessage(typeof(SubFaultDetail), string.Format(" Wrong FaultCode {0} for Vesrion 6", SrcFaultCode));
                }
            }
            else
            {
                CallLogger.LogMessage(typeof(SubFaultDetail), string.Format(" Wrong Code Vesrion {0}", codeVersion));
            }

       

        }

        public void ConvertFaultType()
        {
            FaultStatus = srcProeprtyDataString.Substring(6, 2);

            if (codeVersion == "00" || codeVersion == "01")
            {
                string searchKey = string.Format("[{0}][{1}]",codeVersion,FaultStatus);
                TroubleExplain =  ZICBaseModel.TroubleExplain.Find( s =>s.StartsWith(searchKey));
                if (!string.IsNullOrEmpty(TroubleExplain))
                {
                    TroubleExplain = TroubleExplain.Replace(searchKey, string.Empty);
                }
            }
            else if (codeVersion == "02")
            {
                int faultCode = Convert.ToInt32(SrcFaultCode, 16);
                TroubleExplain = faultCode % 2 == 0 ? "主动/静态" : "被动 / 偶发";
            }
        }
    }
}
